package team.fourth.combat.log;

import com.alibaba.dubbo.config.annotation.Reference;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

@Component
@Aspect
public class OpLogSaveAspect {

//    @Reference
//    LogService logService;


//    @Pointcut(value = "@annotation(org.springframework.web.bind.annotation.RestController) ")
    @Pointcut("execution(* team.fourth.combat.web.*.*(..))")
    public void operationLog() {
    }


    @Around(value = "operationLog()")
    public Object aroundService(ProceedingJoinPoint pjp) throws Throwable {
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
        Signature signature = pjp.getSignature();

        MethodSignature methodSignature = (MethodSignature)signature;

        Method targetMethod = methodSignature.getMethod();
        RequestMapping annotation = targetMethod.getAnnotation(RequestMapping.class);

//        Method realMethod = pjp.getTarget().getClass().getDeclaredMethod(signature.getName(), targetMethod.getParameterTypes());
        Object obj;
        try {
            Object[] args = pjp.getArgs(); // 请求接收的参数args


            //

            obj = pjp.proceed();

        } catch (Throwable t) {
            throw t;
        }

        //
        return obj;

    }


}
